/**
 * 
 */
package javas.system;

import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author lionbule
 * 
 */
public class MyClassLoader extends URLClassLoader {
	public MyClassLoader(URL[] urls) {
		super(urls);
	}
	
	public MyClassLoader(URL[] urls, ClassLoader parent) {
		super(urls, parent);
	}

	@Override
	public final Class<?> loadClass(String name) throws ClassNotFoundException {
		return loadClass(name, false);
	}

	@Override
	public synchronized Class<?> loadClass(String name, boolean resolve)
			throws ClassNotFoundException {
		Class<?> clazz = null;

		
		if(name.startsWith("javas.system")){
	        clazz = findLoadedClass(name);
	        if (clazz != null) {
	            if (resolve)
	                resolveClass(clazz);
	            return (clazz);
	        }

	        clazz = findClass(name);
	        if (clazz != null) {
	            if (resolve)
	                resolveClass(clazz);
	            return (clazz);
	        }		    
		}

		ClassLoader loader = this.getParent();
		if (loader != null) {
			clazz = loader.loadClass(name);
			if (clazz != null) {
				if (resolve)
					resolveClass(clazz);
				return (clazz);
			}
		}
		throw new ClassNotFoundException(name);
	}

	@Override
    public URL getResource(String name) {
        URL url = findResource(name);
        if (url == null) {
            ClassLoader parent = this.getParent();
            if (parent != null) {
                url = parent.getResource(name);
            } else {
                url = getSystemResource(name);
            }
        }
        return url;
    }
    
}
